package in.muton.edushare.url;

import in.muton.edushare.EduShareSession;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class EduShareURLPanel extends Panel {

	private static final long serialVersionUID = 4563681286929266097L;

	private String urlText;

	public EduShareURLPanel(String id) {
		super(id);
	}

	public EduShareURLPanel(String id, IModel<?> model) {
		this(id, model, null);
	}

	public EduShareURLPanel(String id, IModel<?> model, String tabType) {
		super(id, model);
		PageParameters params = EntityPageParams.getParams(model, tabType);
		String text = "url";
		Class<? extends WebPage> pageClass = null;
		boolean isUserLoggedIn = EduShareSession.get().isLoggedIn();

		if (model.getObject() instanceof Universities) {
			text = ((Universities) model.getObject()).getUniversityName();
			if(isUserLoggedIn)
				pageClass = URLLoader.getPageClass(URLConstants.UNIVERSITY_PROFILE_PATH);
			else
				pageClass= URLLoader.getPageClass(URLConstants.UNIV_PUBLIC_PAGE_HOME);
			
		} else if (model.getObject() instanceof Departments) {
			Departments dept = (Departments) model.getObject();
			text = dept.getDeptName();
			if(isUserLoggedIn)
				pageClass = URLLoader.getPageClass(URLConstants.DEPARTMENT_PROFILE_PATH);
			else
				pageClass = URLLoader.getPageClass(URLConstants.DEPT_PUBLIC_PAGE_HOME);
			
		} else if (model.getObject() instanceof Courses) {
			Courses course = (Courses) model.getObject();
			text = course.getCourseName();
			if(isUserLoggedIn)
				pageClass = URLLoader.getPageClass(URLConstants.COURSE_PROFILE_PATH);
			else
				pageClass = URLLoader.getPageClass(URLConstants.COURSE_PUBLIC_PAGE_HOME);
			
		} else if(model.getObject() instanceof CourseInstances){
			Courses course = ((CourseInstances) model.getObject()).getCourse();
			text = course.getCourseName();
			if(isUserLoggedIn)
				pageClass = URLLoader.getPageClass(URLConstants.COURSE_PROFILE_PATH);
			else
				pageClass = URLLoader.getPageClass(URLConstants.COURSE_PUBLIC_PAGE_HOME);
			
		}else if (model.getObject() instanceof Users) {
			Users user = (Users) model.getObject();
			text = user.getCompleteUserName();
			if(isUserLoggedIn)
				pageClass = URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH);
			else
				pageClass = URLLoader.getPageClass(URLConstants.USER_PUBLIC_PAGE_SUMMARY);
			
		}else if (model.getObject() instanceof Groups) {
			Groups g = (Groups) model.getObject();
			text = g.getGroupName();
			pageClass = URLLoader.getPageClass(URLConstants.GROUPS_NEWS);
		} else {
			throw new EduShareRuntimeException(
					"Cant create a home-page url for object of type: "
							+ model.getObject().getClass());
		}

		BookmarkablePageLink<Void> link = new BookmarkablePageLink<Void>("url",
				pageClass, params);
		add(link);

		final String modelText = text;
		link.add(new Label("txt", new IModel<String>() {
			private static final long serialVersionUID = 2060567833171611514L;

			public String getObject() {
				return StringUtil.isValid(EduShareURLPanel.this.urlText) ? EduShareURLPanel.this.urlText
						: modelText;
			}

			public void setObject(String arg0) {
			}

			public void detach() {
			}
		}).setRenderBodyOnly(true));
	}

	public EduShareURLPanel setUrlText(String urlText) {
		this.urlText = urlText;
		return this;
	}

	public String getUrlText() {
		return urlText;
	}

}
